#!/bin/bash

set -e
pushd $(cd $(dirname ${0})/..; pwd) > /dev/null

bold() {
    echo "$(tty -s && tput bold)$1$(tty -s && tput sgr0)" 
}

clean() {
    bold "mnn clean"
    rm -rf "./third_party/src/mnn"
}

sync() {
    bold "mnn sync"
    [ -d "./third_party/src/mnn" ] || git clone --quiet https://github.com/alibaba/MNN.git "./third_party/src/mnn"
    pushd "./third_party/src/mnn" > /dev/null
    git pull --quiet --prune
    popd > /dev/null
}

schema() {
    bold "mnn schema"
    [[ $(grep -U $'\x0D' ./src/mnn-schema.js) ]] && crlf=1
    node ./tools/flatc.js --root mnn --out ./src/mnn-schema.js ./third_party/src/mnn/schema/default/MNN.fbs
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./src/mnn-schema.js ./src/mnn-schema.js
    fi
}

while [ "$#" != 0 ]; do
    command="$1" && shift
    case "${command}" in
        "clean") clean;;
        "sync") sync;;
        "schema") schema;;
    esac
done
